const app = getApp()
const { fromUtils, baseUtils } = require("../../utils/index")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgInfo: {
      fileList: {},
      fileListTwo: {},
    },
    infoList: [
      {
        title: '姓名',
        value: '宇少'
      },{
        title: '开户号',
        value: '',
        valueType: 'input',
        attr: {
          type: 'text',
          placeholder: '识别银行卡号',
          maxlength: '11',
        }
      },{
        title: '开户地址',
        value: '',
        valueType: 'address',
        attr: {
          index: 0,
          range: [],
          placeholder: '请选择开户行地址',
        }
      },{
        title: '开户行',
        value: '',
        valueType: 'address',
        attr: {
          index: 0,
          range: [],
          placeholder: '请选择银行及支行信息',
        }
      },{
        title: '联系人手机号',
        value: '',
        valueType: 'input',
        attr: {
          type: 'number',
          placeholder: '请输入银行预留手机号',
          maxlength: '11',
          checkType:'checkIphone'
        }
      },{
        titleType: 'input',
        value: '',
        attr: {
          type: 'number',
          placeholder: '请输入验证码',
          maxlength: '6',
          checkType:'checkCode'
        },
        valueType: 'button',
        buttonAttr:{
          cont: '获取验证码',
          isButtonAct: true
        }
      }
    ],
    isAction: false,
    type: 'shopAddress'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  onAfterRead(event){
    const type = event.currentTarget.dataset.type
    const file = event.detail.file;
    this.data.imgInfo[type] = {url: file.url}
    this.setData({
      imgInfo: this.data.imgInfo
    })
  },
  onDelImg(e){
    const type = e.currentTarget.dataset.type
    const that = this
    wx.showModal({
      title: '提示',
      content: '确定要删除这张图片吗?',
      success (res) {
        if (res.confirm) {
          that.data.imgInfo[type] = {}
          that.setData({
            imgInfo: that.data.imgInfo
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onInputBlur(e){
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    const checkType = this.data.infoList[index].attr.checkType
    const msg = checkType && fromUtils[checkType](value)
    if(msg){
      app.showMsg(msg)
      this.data.infoList[index].value = ''
      this.data.isAction = false
    }else{
      this.data.infoList[index].value = value
      this.data.isAction = !this.data.infoList.find(i => i.value === '')
    }
    this.setData({
      infoList: this.data.infoList,
      isAction: this.data.isAction
    })
  },
  onSendCode(){
    if(this.data.infoList[4].value !== ''){
      baseUtils.countDown(60, (res)=>{
        this.data.infoList[5].buttonAttr.cont = res
        this.data.infoList[5].buttonAttr.isButtonAct = res == '重新发送' ? true : false
        this.setData({
          infoList: this.data.infoList
        })
      })
    }else{
      this.data.type == 'email' ? app.showMsg('请输入新邮箱') : app.showMsg('请输入新手机号')
    }
  },
  onSend(){
    const arr = this.data.infoList.filter(i => i.value !== '').map(s => s.value)
    console.log(arr)
    if(!this.data.isAction) return
    // const arr = this.data.infoList.filter(i => i.value !== '').map(s => s.value)
    // console.log(arr)
  }
})